<?php
/**
 * 绩效过了自评日的，但是还没打分的自动2分处理
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Tools_SendMail');
class Oa_Job_Jx_AutoSetScore extends Oa_Job_Abstract {

    private $read_db = "jx_slave";//读数据库
    private $writedb = "jx_master";//写数据库

    public function run($params){
        $today = date('Ymd');


        if($params['day']){
            $this->run_day = $params['day'];
        }else{//指定日期的前一次
            $this->run_day = date('Ym');
        }
        $do_day = $this->get_score_day();

        if($today != $do_day){
            $this->log('不用处理...');exit;
        }
        $this->all_users();
        //取所有需要考核的人
        $this->get_performance($do_day);
    }

    private function get_performance($date){
        $sql = "SELECT * FROM user_performance where serachMonth = ".$date;
        $user_list = $this->get_db_rows($sql,$this->writedb,array());
        $last = array();
        foreach($user_list as $val){
            if(!empty($this->mk[$val['userId']])){
                continue;
            }
            //需要考评的人,自己和主管没有打分
            if(!$val['personScore'] && !$val['treasurerScore']){
                $sql= "update user_performance set treasurerScore=2 ,adjustScore=2 where id=".$val['id'];
                $this->execute( $sql,array(), $this->writedb);
                $last[] = $val['userId'];
            }
        }
        if(!empty($last)){//有自动打分的，通知下hr
            $sql = "SELECT * FROM user_info_kp where user_id IN (".implode(',',$last).")";
            $list = $this->get_db_rows($sql,$this->writedb,array());
            $str = array("姓名\t工号\t上级工号");
            if(!empty($list)){
                foreach($list as $val){
                    $str[] = $val['name']."\t".$val['code']."\t".$val['superior_code'];
                }
            }
            if(!empty($str)){//发邮件了
                $mail_tool = Tools_SendMail::get_instance();
                $mailcontent = implode("<br>", $str);
                $mail_tool->send('fionayu@anjukeinc.com','绩效自动打2分提醒',$mailcontent);
            }
        }
    }

    private $mk= array();//免考
    private $need = array();
    private function all_users(){
        $sql = "SELECT * FROM user_info_kp where 1";
        $users = $this->get_db_rows($sql,$this->writedb,array());
        foreach($users as $val){
            if($val['flag']){
                $this->mk[$val['user_id']] = $val;
            }else{
                $this->need[$val['user_id']] = $val;
            }
        }
    }
    /**
     * 到了评分日上一次的日期进行操作
     * @return string
     */
    private function get_score_day(){
        $sql = "SELECT * FROM date_step_info where revicwMonth<".$this->run_day." AND step_status=3 order by id desc limit 1 ";
        $ckdate= $this->get_db_row($sql,$this->writedb,array());
        if(empty($ckdate)){
            $this->log('没有设置评分日');exit;
        }
        $date = strtotime($ckdate['step_date']);
        $date +=86000;
        return date('Ymd',$date);
    }
}